package com.ibm.spatiotemp.server.datamodel.gpsdata.dynamodb;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.ibm.spatiotemp.server.datamodel.gpsdata.TSPoint;
import com.ibm.spatiotemp.server.datamodel.gpsdata.TSPointResultSet;
import com.ibm.spatiotemp.server.datasources.DataSchema;

public class TSResultSet4Dynamodb extends TSPointResultSet{

	public static Logger Logger1 = Logger.getLogger(TSResultSet4Dynamodb.class);
	
	protected List<Map<String, AttributeValue>> inner_results;
	protected Iterator<Map<String, AttributeValue>> dataiterator;
	protected DataSchema resultschema;
	
	public TSResultSet4Dynamodb(List<Map<String, AttributeValue>> rescollection, DataSchema schema){
		if(rescollection == null)
		{
			Logger1.error("null result set");
			return;
		}
		this.inner_results = rescollection;
		this.dataiterator = this.inner_results.iterator();
		this.resultschema = schema;
	}
	
	
	@Override
	public void close() throws Exception {
		inner_results = null;
	}

	@Override
	public TSPoint getNext() throws Exception {
		Map<String, AttributeValue> resultelement = null;
		if(dataiterator.hasNext()){
			resultelement = dataiterator.next();
			String tsid = resultelement.get(resultschema.getMovobj_colname()).getS();
			double lon = Double.parseDouble(resultelement.get(resultschema.getX_colname()).getN());
			double lat = Double.parseDouble(resultelement.get(resultschema.getY_colname()).getN());
			long timestamp = Long.parseLong(resultelement.get(resultschema.getTs_colname()).getN());
			TSPoint point = new TSPoint( tsid,  lon,  lat,  timestamp);
			return point;
		}
		else{
			return null;
		}
		
	}

}
